﻿using Anke.SHManage.BLL;
using Anke.SHManage.Model;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.CM.Controllers
{
    public class RepairMaintainController : Controller
    {
        //
        // GET: /CM/RepairMaintain/
        //整修保养页面
        public ActionResult RepairMaintainList(string PassCenterCode, string PassStationCode, string PassPlateNumber, string PassCarNumber)
        {
            E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
            this.ViewData["em"] = em;
            string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
            this.ViewData["selfWorkCode"] = selfWorkCode;
            string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
            this.ViewData["selfCenterID"] = selfCenterID;
            string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;
            this.ViewData["selfStationID"] = selfStationID;
            UserOperateContext userOperateContext = new UserOperateContext();
            bool Insert = userOperateContext.getGongneng(1261);//判断登录人有无“新增”车辆整修保养权限
            if (Insert)
            {
                this.ViewData["RolePermissionInsert"] = "Insert";//登录人有新增车辆整修保养权限
            }
            this.ViewData["start"] = DateTime.Now.AddDays(1 - DateTime.Now.Day).ToString("yyyy-MM-dd 00:00:00");
            this.ViewData["end"] = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd 00:00:00");
            this.ViewData["name"] = UserOperateContext.Current.Session_UsrInfo.Name;
            this.ViewData["time"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            if (PassCenterCode != "--请选择--" && PassCenterCode != "" && PassCenterCode != null)
            { this.ViewData["txtCenterCode"] = PassCenterCode; };
            if (PassStationCode != "--请选择--" && PassStationCode != "" && PassStationCode != null)
            { this.ViewData["txtStationCode"] = PassStationCode; };
            if (PassPlateNumber != "--请选择--" && PassPlateNumber != "" && PassPlateNumber != null)
            { this.ViewData["txtPlateNumber"] = PassPlateNumber; };
            if (PassCarNumber != "--请选择--" && PassCarNumber != "" && PassCarNumber != null)
            { this.ViewData["txtCarNumber"] = PassCarNumber; };
            return View();
        }

        // 查询整修保养列表信息  尤浩 2015.12.1
        [HttpPost]
        public ActionResult DataLoad()
        {
            try
            {
                string start = Request.Form["start"].ToString();
                string end = Request.Form["end"].ToString();
                string center = Request.Form["center"].ToString();
                string station = Request.Form["station"].ToString();
                string actualCode = Request.Form["actualCode"].ToString();
                string ambulanceNumber = Request.Form["ambulanceNumber"].ToString();
                string sendConfirmedPerson = Request.Form["sendConfirmedPerson"].ToString();
                string repairPerson = Request.Form["repairPerson"].ToString();
                string inspector = Request.Form["inspector"].ToString();
                string acceptor = Request.Form["acceptor"].ToString();
                string repairType = Request.Form["repairType"].ToString();

                //获取页容量
                int rows = int.Parse(Request.Form["rows"]);
                //获取请求的页码
                int page = int.Parse(Request.Form["page"]);
                E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
                string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
                string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
                string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;

                var list = new C_RepairMaintainBLL().GetRepairMaintainList(page, rows, start, end, center, station, actualCode, ambulanceNumber
                                                                             , sendConfirmedPerson, repairPerson, inspector, acceptor
                                                                             , repairType, em, selfWorkCode, selfCenterID, selfStationID);
                JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
                //return Json(new { total = total, rows = list }, "appliction/json", JsonRequestBehavior.AllowGet);
                return j;
            }

            catch (Exception e)
            {
                Dictionary<string, string> dict = new Dictionary<string, string>();
                dict.Add("InfoID", "0");
                dict.Add("InfoMessage", e.Message);
                return this.Json(dict);
            }
        }

        // 修改   尤浩 2015.12.1  
        [HttpPost]
        public ActionResult RepairMaintainEdit()
        {

            string sDetailInfo = Request.Form["sRepairInfo"].ToString();
            C_RepairMaintain model = JsonHelper.GetJsonInfoBy<C_RepairMaintain>(sDetailInfo);
            C_OilCardLink info = new C_OilCardRefuelBLL().GetActualCodeLinkList(model.ActualCode);
            model.AmbulanceCode = info.AmbulanceCode;
            model.CenterID = Convert.ToInt32(info.CenterID);
            model.StationID = info.StationID;
            model.FillTime = DateTime.Now;
            int res = new C_RepairMaintainBLL().Modify(model, "CenterID", "StationID", "AmbulanceCode", "ActualCode", "AmbulanceNumber", "RepairType", "IsChangeRoadCode", "InMileage", "OutMileage", "InOilMass", "OutOilMass",
                       "IsSpareWheel", "IsDrivingLicense", "SendConfirmedTime", "CompletionTime", "RepairContent", "SendConfirmedPerson", "RepairPerson", "Inspector", "Acceptor", "Remark", "FillTime", "FillPerson");

            if (res > 0)
                return this.JsonResult(Utility.E_JsonResult.OK, "修改成功！", null, null);
            else
                return this.JsonResult(Utility.E_JsonResult.Error, "修改失败", null, null);
        }

        // 新增   尤浩 2015.12.1  
        [HttpPost]
        public ActionResult RepairMaintainAdd()
        {
            string sDetailInfo = Request.Form["sRepairInfo"].ToString();
             C_RepairMaintain info = JsonHelper.GetJsonInfoBy<C_RepairMaintain>(sDetailInfo);
            //随机生成一个ID的值   
            info.ID = System.Guid.NewGuid();
            info.FillTime = DateTime.Now;
            if(info.AmbulanceNumber==null)
            { info.AmbulanceNumber = ""; }
            int res = new C_RepairMaintainBLL().Add(info);
            if (res > 0)
                return this.JsonResult(Utility.E_JsonResult.OK, "保存成功！", null, null);
            else
                return this.JsonResult(Utility.E_JsonResult.Error, "保存失败", null, null);
        }
    }
}
